package org.example.conf;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisNode;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.util.StringUtils;
import redis.clients.jedis.JedisPoolConfig;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

@Configuration
public class RedisConfig {


    @Value("${spring.redis.nodes}")
    private String redisNodes;
    @Value("${spring.redis.passwd}")
    private String redisPasswd;


    @Bean
    public JedisPoolConfig initJedisPoolConfig(){
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        return jedisPoolConfig;
    }

    @Bean
    public JedisConnectionFactory jedisConnectionFactory(JedisPoolConfig jedisPoolConfig){
        String[] hostAndPorts = redisNodes.split(",");
        JedisConnectionFactory jedisConnectionFactory = null;
        if(hostAndPorts.length==1){
            //单机版
            String host = hostAndPorts[0].split(":")[0];
            int port = Integer.valueOf(hostAndPorts[0].split(":")[1]);
            jedisConnectionFactory = new JedisConnectionFactory();
            jedisConnectionFactory.setPoolConfig(jedisPoolConfig);
            jedisConnectionFactory.setDatabase(1);
            jedisConnectionFactory.setHostName(host);
            jedisConnectionFactory.setPassword(redisPasswd);
            jedisConnectionFactory.setPort(port);
            jedisConnectionFactory.setTimeout(5000);
            jedisConnectionFactory.afterPropertiesSet();
        }else{
            //集群版
            RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration();
            Set<RedisNode> nodes = new HashSet<RedisNode>();
            for (String ipPort:hostAndPorts){
                String[] ipAndPort = ipPort.split(":");
                nodes.add(new RedisNode(ipAndPort[0].trim(),Integer.valueOf(ipAndPort[1])));
            }
            redisClusterConfiguration.setClusterNodes(nodes);
            redisClusterConfiguration.setMaxRedirects(200);
            jedisConnectionFactory = new JedisConnectionFactory(redisClusterConfiguration,jedisPoolConfig);
            jedisConnectionFactory.setPassword(redisPasswd);
            jedisConnectionFactory.afterPropertiesSet();
        }
        return jedisConnectionFactory;
    }
    public RedisClusterConfiguration getRedisClusterConfiguration() {

        RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration();

        String[] serverArray = redisNodes.split(",");
        Set<RedisNode> nodes = new HashSet<RedisNode>();
        for (String ipPort : serverArray) {
            String[] ipAndPort = ipPort.split(":");
            nodes.add(new RedisNode(ipAndPort[0].trim(), Integer.parseInt(ipAndPort[1])));
        }
        redisClusterConfiguration.setClusterNodes(nodes);
        if(!StringUtils.isEmpty(redisPasswd)){
          /*  RedisPassword pwd = RedisPassword.of(redisServerPassword);
            redisClusterConfiguration.setPassword(pwd);*/
        }

        return redisClusterConfiguration;
    }



    /**
     * 配置RedisTemplate实例
     * @param factory
     * @return
     */
    @Bean
    public RedisTemplate<Serializable, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
        RedisTemplate<Serializable, Object> template = new RedisTemplate<Serializable, Object>();
        template.setConnectionFactory(connectionFactory);
        template.afterPropertiesSet();
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new ObjectRedisSerializer());//自定义
        //template.setValueSerializer(new Jackson2JsonRedisSerializer(Object.class));//redis提供现有的
        return template;
    }
}
